package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.experimental.PackagePrivate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理相关接口")
public class DishController {

    //注入DishService
    @Autowired
    private DishService dishService;
    private DishMapper dishMapper;

    /**
     * 新增菜品
     * @return
     */
    @ApiOperation(value = "新增菜品")
    @PostMapping()
    @CacheEvict(cacheNames = "dishCache",key="#dishDTO.categoryId")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);

        //调用service层新增菜品
        dishService.save(dishDTO);

        //返回结果给前端
        return Result.success();
    }

    /**
     * 分页查询菜品
     * @return
     */
    @ApiOperation(value = "分页查询菜品")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品：{}",dishPageQueryDTO);

        //调用service层分页查询菜品
        PageResult pageResult = dishService.page(dishPageQueryDTO);

        //返回结果给前端
        return Result.success(pageResult);
    }

    /**
     * 根据id批量删除菜品
     * @return
     */
    @ApiOperation(value = "根据id批量删除菜品")
    @DeleteMapping()
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        log.info("根据id批量删除菜品：{}",ids);

        //调用service层根据id批量删除菜品
        dishService.delete(ids);

        //返回结果给前端
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品：{}",id);

        //调用service层根据id查询菜品
        DishVO dishVO = dishService.getById(id);

        //返回结果给前端
        return Result.success(dishVO);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        log.info("根据分类id查询菜品：{}",categoryId);

        //调用service层根据分类id查询菜品
        List<Dish> list = dishService.list(categoryId);

        //返回结果给前端
        return Result.success(list);
    }

    /**
     * 菜品起售或停售
     * @return
     */
    @ApiOperation(value = "菜品起售或停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("菜品起售或停售:{}",status);

        //调用service对象方法，菜品起售或停售
        dishService.startOrStop(status,id);

        //返回结果
        return Result.success();
    }
}
